#!/usr/bin/perl


################################################################
# Author   : DebugProject_Bourdin
# Created  : 06/06/2007
# Modified : 06/06/2007
# Script   : delete files used for previous comparison on the
#	     same executions (executions concerned by the
#	     parameters folders : exec1Path and exec2Path)
################################################################

#check the command line
if(scalar(@ARGV) !=2){die "[way to use : <exec1Path> <exec2Path>]"}

$exec1Path = $ARGV[0];
$exec2Path = $ARGV[1];
$compare1 = $exec1Path."compare.html";
$compare2 = $exec2Path."compare.html";

#check that exec1Path exists
if(!(-d $exec1Path)){die "[directory <exec1Path> doesn't exist]"}

#check that exec2Path exists
if(!(-d $exec2Path)){die "[directory <exec2Path> doesn't exist]"}

$toRemove11 = "";
$toRemove12 = "";

#check in exec1Path and exec2Path
if(-e $compare1){
	#Openinning handle
	open(CMP1,"<$compare1") || die "[Can't open $compare1]";
	#while there are lines in CMP1
	while($li1=<CMP1>){
		#to get the path of the first previous execution with exec1Path (that might be exec1Path !)
		if($li1=~/^\s*\<FRAME\s+name\=\"fcomp1\"\s+src\=\"(.*)out.*$/){
			$toRemove11 = $1;
		}
		#to get the path of the second previous execution with exec1Path (that might be exec1Path !)
		if($li1=~/^\s*\<FRAME\s+name\=\"fcomp2\"\s+src\=\"(.*)out.*$/){
			$toRemove12 = $1;
		}
	}
	#to delete the old files of the previous executions
	unlink($toRemove11."compare.html");
	unlink($toRemove11."out.xml");
	unlink($toRemove12."compare.html");
	unlink($toRemove12."out.xml");
	
}

if(-e $compare2){
	#Openinning handles
	open(CMP2,"<$compare2") || die "[Can't open $compare2]";
	#while there are lines in CMP1
	while($li2=<CMP2>){
		#to get the path of the first previous execution with exec2Path (that might be exec2Path !)
		if($li2=~/^\s*\<FRAME\s+name\=\"fcomp1\"\s+src\=\"(.*)out.*$/){
			$toRemove21 = $1;
		}
		#to get the path of the second previous execution with exec2Path (that might be exec2Path !)
		if($li2=~/^\s*\<FRAME\s+name\=\"fcomp2\"\s+src\=\"(.*)out.*$/){
			$toRemove22 = $1;
		}
	}
	#if the files haven't been deleted in the first part of this script, we delete them now
	if(($toRemove21 ne $toRemove11) && ($toRemove21 ne $toRemove12)){
		unlink($toRemove21."compare.html");
		unlink($toRemove21."out.xml");
	}
	if(($toRemove22 ne $toRemove11) && ($toRemove22 ne $toRemove12)){
		unlink($toRemove22."compare.html");
		unlink($toRemove22."out.xml");
	}
}
